package com.shujia.scala

object Demo4Class {
  def main(args: Array[String]): Unit = {

    val dog = new Dog("布丁", 3)

    //样例类可以直接通过类名创建对象
    val d1 = Dog("旺财", 3)


    println(dog)

    Pig.print()


    val pig:Pig = Pig("001",23)

  }
}

/**
  * 样例类  简化代码
  *
  */

case class Dog(name: String, age: Int)


class Pig(name: String, age: Int) {
  override def toString: String = {
    name + "\t" + age
  }
}

//  伴生对象
object Pig {
  def print(): Unit = {
    println("对象的方法")
  }

  /**
    *
    * 特殊方法 直接对象名加（)调用
    *   Pig()
    *
    */
  def apply(name: String, age: Int): Pig = {
    new Pig(name, age)
  }
}


